package cn.dai.dao;

import cn.dai.pojo.Role;
import cn.dai.util.JdbcUtils;

import java.util.List;

/**
 * @author ArkD42
 * @file RBAC
 * @create 2020 - 06 - 11 - 17:02
 */
public class RoleDaoImpl implements RoleDao{

    @Override
    public List<Role> queryRoleList() {
        return JdbcUtils.queryToList(
                JdbcUtils.getConnection(),
                Role.class,
                "SELECT * FROM role",
                null
        );
    }

    @Override
    public Role queryRoleById(Integer id) {
        return JdbcUtils.queryOne(
                JdbcUtils.getConnection(),
                Role.class,
                "SELECT * FROM role WHERE r_id = ? ",
                new Object[]{id}
        );
    }

    @Override
    public int insertRole(Role role) {
        return JdbcUtils.update(
                JdbcUtils.getConnection(),
                new StringBuffer("INSERT INTO role VALUES").
                        append("(DEFAULT,?,?)").toString(),
                new Object[]{
                    role.getR_name(),
                        role.getR_description()
                }
        );
    }

    @Override
    public int deleteRoleById(Integer id) {
        return JdbcUtils.update(
                JdbcUtils.getConnection(),
                "DELETE FROM role WHERE r_id = ?",
                new Object[]{id}
        );
    }

    @Override
    public int updateRoleById(Role role) {
        return JdbcUtils.update(
                JdbcUtils.getConnection(),
                new StringBuffer("UPDATE role SET ").
                        append("r_name = ?, r_description = ?").
                        toString(),
                new Object[]{
                        role.getR_name(),
                        role.getR_description()
                }
        );
    }
}
